<template>
	<view style="display: flex;flex-direction: column;align-items: center;">
		<image style="width: 100%;position: absolute;top: 0;z-index: 1;"
			src="https://daiyue.gzzhenyi.net/static/image/order/svipBg@4x.png" mode="widthFix"></image>
		<view class="input_card">
			<view class="price_jiage">
				<text>￥{{amount}}</text>
				<text>￥{{vip.upgrade_amount}}</text>
			</view>
			<view class="input">
				<van-progress color="#F2B677" track-color="#fff" stroke-width="8px" :show-pivot="false"
					:percentage="Math.floor((amount * 100) / 3000)>100?100:Math.floor((amount * 100) / 3000)" />
			</view>
			<view class="add_msg">
				目前累计购物金额：<text>￥{{amount}}</text>
			</view>
			<text class="tips">{{vip.vip_rule}}</text>
			<text class="vip_price">享受会员价</text>
		</view>
		<view class="pay">
			<view class="ya">
				<image style="position: absolute;z-index: 3;top: 0;left: 0;"
					src="https://daiyue.gzzhenyi.net/static/image/order/ya@4x.png" mode="widthFix"></image>
				<text class="threeVip" style="margin-top: 46rpx;">￥{{vip.deposit_amount}}押金</text>
				<text class="threeVip">可成为三年会员</text>
				<view class="smollTips" style="margin:0 auto;margin-top: 28rpx;width: 420rpx;" v-html="vip.deposit_rule"></view>
			</view>
			<view class="weipay">
				<view class="left">
					<image style="width: 30rpx;height: 30rpx;"
						src="https://daiyue.gzzhenyi.net/static/image/order/wei@4x.png" mode="">
					</image>
					<text>微信支付</text>
				</view>
				<van-checkbox checked-color="#FFC990" icon-size="15px" :value="defaulta"
					@change="onChange"></van-checkbox>
			</view>
			<view class="btn">
				<view class="button_pay" @click="goPay">
					立即支付
				</view>
			</view>
			<view class="agree">
				开通即视为同意<text @tap="toProtocol">《孕动伊栈会员-用户协议》</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		userBuy,
		getVip,
		getUserInfo
	} from '@/api/api'
	import {
		mapState,
		mapMutations
	} from "vuex";
	export default {
		data() {
			return {
				defaulta: true,
				vip:{},
				amount:0
			}
		},
		computed: {
			...mapState(["user"])
		},
		onLoad() {
			this.getData();
		},
		methods: {
			...mapMutations('user', ['changeMobile']),
			getData(){
				getVip().then(res=>{
					if(res.code == 1){
						this.vip = res.data;
					}
				})
				getUserInfo().then(res => {
					if (res.code == 1) {
						this.amount = res.data.amount
					}
				})
			},
			onChange() {
				this.defaulta = !this.defaulta
			},
			toProtocol() {
				uni.navigateTo({
					url: "/pages/member/protocol"
				})
			},
			goPay() {
				console.log(this.user)
				if(!this.user.mobile){
					uni.showToast({
					    title: '请输入手机号',
					    icon: 'none'
					})
					uni.navigateTo({
						url: "/pages/setting/mobile"
					})
				}else{
					userBuy({
						type: 'vip'
					}).then(res => {
						if (res.code == 1) {
							uni.requestPayment({
								provider: 'wxpay',
								"timeStamp": res.data.timeStamp, //时间戳
								"nonceStr": res.data.nonceStr, //随机字符串
								"package": res.data.package, //接口返回的prepay_id
								"signType": res.data.signType, //签名算法，需要与后台下单时一致
								"paySign": res.data.paySign, // 签名
					
								success: function(res) {
									console.log('success:' + JSON.stringify(res));
									uni.showToast({
										title: '支付成功',
										icon: 'none'
									});
									setTimeout(()=>{
										uni.switchTab({
											url:"/pages/user/user"
										})
									},800)
								},
								fail: function(err) {
									console.log('fail:' + JSON.stringify(err));
								}
							});
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input_card {
		margin-top: 88rpx;
		position: relative;
		z-index: 2;
		width: 598rpx;
		border-radius: 20rpx;
		background: #FFFFFF;
		padding: 26rpx 56rpx;

		.price_jiage {
			display: flex;
			justify-content: space-between;

			text {
				font-family: PingFang SC;
				font-size: 24rpx;
				color: #704B1B;
			}
		}

		.input {
			margin-top: 10rpx;

			::v-deep .van-progress {
				border: 2rpx solid #F2B677;
			}
		}

		.add_msg {
			margin-top: 46rpx;
			font-size: 34rpx;
			text-align: center;
			color: #704B1B;
			font-weight: 400;

			text {
				font-weight: bold;
			}

		}

		.tips {
			margin-top: 40rpx;
			text-align: center;
			color: #704B1B;
			font-weight: 400;
			font-size: 26rpx;
			display: inline-block;
			width: 100%;
		}

		.vip_price {
			margin-top: 10rpx;
			font-weight: 400;
			font-size: 26rpx;
			text-align: center;
			color: #704B1B;
			display: inline-block;
			width: 100%;
		}
	}

	.pay {
		width: 650rpx;
		// height: 735px;
		border-radius: 20rpx;
		background: #FFFFFF;
		padding: 48rpx 30rpx;
		position: relative;
		z-index: 3;
		margin-top: 30rpx;

		.ya {
			position: relative;
			width: 650rpx;
			height: 290rpx;
			display: flex;
			flex-direction: column;

			.threeVip {
				position: relative;
				z-index: 5;
				display: inline-block;
				text-align: center;
				font-size: 36rpx;
				font-weight: 600;
				margin-top: 4rpx;
				color: #704B1B;
			}

			.smollTips {
				position: relative;
				z-index: 5;
				display: inline-block;
				text-align: center;
				font-size: 24rpx;
				color: #704B1B;
				margin-top: 4rpx;
			}

		}

		.weipay {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 100rpx;

			.left {
				display: flex;
				align-items: center;

				text {
					font-size: 30rpx;
					line-height: 30px;
					color: #333333;
					margin-left: 10rpx;
				}
			}
		}

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 58rpx;

			.button_pay {

				width: 600rpx;
				height: 100rpx;
				border-radius: 50rpx;

				background: #FFC990;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.agree {
			display: flex;
			align-items: center;
			margin-top: 20rpx;
			font-size: 24rpx;
			color: #37363D;

			text {
				display: inline-block;
				margin-left: 10rpx;
				color: #EBB234;
				font-weight: normal;
			}
		}
	}
</style>